package com.zheng.cahce.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zheng.cahce.model.UserEntity;
import com.zheng.cahce.service.CacheService;

@Controller
public class CacheTestController {
	@Autowired
	private CacheService cacheService;
	/**
	 * 
	 * @Description: 测试是否启动成功 
	 * @author: zhengchuanqing
	 * @date: 2019年3月6日 下午10:57:09  
	 * @param: 
	 * @return: Map<String,Object>
	 * @throws
	 */
	@ResponseBody
    @RequestMapping("/test")
    public Map<String, Object> test(){
    	Map<String, Object> map = new HashMap<>();
    	map.put("data", "Holle Word");
        return map;
    }

	@ResponseBody
    @RequestMapping("/getCache")
    public Map<String, Object> getCache(@RequestParam("key") String key){
    	Map<String, Object> map = new HashMap<>();
    	Object object = cacheService.getValue(key);
    	map.put("data", object);
        return map;
    }

    @ResponseBody
    @RequestMapping("/setCache")
    public Map<String, Object> setCache(@RequestParam("key") String key, @RequestParam("value") String value) throws Exception{
    	Map<String, Object> map = new HashMap<>();
    	boolean state = cacheService.setValue(key, value);
    	map.put("data", state ? "success" : "error");
        return map;
    }
    
    @ResponseBody
    @RequestMapping("/getCacheEntity")
    public Map<String, Object> getCacheEntity(@RequestParam("key") String key){
    	Map<String, Object> map = new HashMap<>();
    	UserEntity object = (UserEntity)cacheService.getValue(key);
    	map.put("data", object);
    	return map;
    }

    @ResponseBody
    @RequestMapping("/setCacheEntity")
    public Map<String, Object> setCacheEntity(@RequestParam("key") String key) throws Exception{

    	UserEntity userEntity = new UserEntity();
    	userEntity.setUserName(key);
    	userEntity.setPassword(key+"_0001");
    	userEntity.setSex(60);
    	
    	Map<String, Object> map = new HashMap<>();
    	boolean state = cacheService.setValue(key, userEntity);
    	map.put("data", state ? "success" : "error");
    	return map;
    }

    @SuppressWarnings("unchecked")
	@ResponseBody
    @RequestMapping("/getCacheList")
    public Map<String, Object> getCacheList(@RequestParam("key") String key){
    	Map<String, Object> map = new HashMap<>();
    	List<UserEntity> list = (List<UserEntity>)cacheService.getValue(key);
    	map.put("data", list);
    	return map;
    }
    
    @ResponseBody
    @RequestMapping("/setCacheList")
    public Map<String, Object> setCacheList(@RequestParam("key") String key) throws Exception{
    	List<UserEntity> list = new ArrayList<>();
    	for (int i = 0; i < 5; i++) {
    		UserEntity userEntity = new UserEntity();
        	userEntity.setUserName(key+i);
        	userEntity.setPassword(key+"_000"+i);
        	userEntity.setSex(60+i);
        	list.add(userEntity);
		}
    	Map<String, Object> map = new HashMap<>();
    	boolean state = cacheService.setValue(key, list);
    	map.put("data", state ? "success" : "error");
    	return map;
    }
}
